<template>
  <div class="fixed-table-actionbar">
    <div class="search-bar">
      <el-form :inline="true" :model="formItem" class="form-filter" size="mini">
        <el-form-item>
          <el-select v-model="formItem.username" placeholder="请选择人员" clearable filterable style="width:100%" @change="search">
            <el-option v-for="(item) in availableWorkers" :key="item.username" :label="item.name" :value="item.username" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="formItem.start"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始日期"
            @change="search"
          />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="formItem.end"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择截止日期"
            @change="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button plain icon="el-icon-search" @click="search" />
        </el-form-item>
      </el-form>
    </div>
    <div class="action-bar">
      <el-button size="mini" icon="el-icon-download" @click="exportExcel">导出</el-button>
    </div>
  </div>
</template>

<script>
import { list as getUsers } from '@neu/auth-ui/api/user'
import { getToken } from '@neu/biz-core/utils/auth'
import { formatDate } from '@neu/biz-core/utils/utils'
import { exportReport } from '@neu/dcom-ui/api/workDaily'

// 定义一个查询表单原始项
const formItem = {
  username: '',
  start: null,
  end: null
}
export default {
  name: 'DailyReportTableFilter',
  data() {
    return {
      formItem: {
        ...formItem
      },
      availableWorkers: [],
      action: '',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      fileName: '日报统计导出' + formatDate(new Date())
    }
  },
  created() {
    getUsers({ s_roleCode: 'worker' }).then(response => {
      this.availableWorkers = response.data
    })
  },
  methods: {
    // 查询按钮点击事件处理
    search: function() {
      const data = {
        ...this.formItem
      }
      this.$parent.filter(data)
    },
    // 重置按钮点击事件处理
    reset: function() {
      this.formItem = {
        ...formItem
      }
      this.search()
    },
    // 导出
    exportExcel() {
      const data = {
        ...this.formItem
      }
      exportReport(data).then(res => {
        const content = res
        const blob = new Blob([content])
        const fileName = this.fileName + '.xlsx'
        // not IE downLoad
        if ('download' in document.createElement('a')) {
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href)
          document.body.removeChild(elink)
        } else {
          navigator.msSaveBlob(blob, fileName)
        }
      })
    }
  }
}
</script>
